//jQuery HTML导出Excel文件(兼容IE及所有浏览器)
function ExportToExcel(filename) {
    //询问框
    layer.confirm('确定导出吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        tableid = "mytable";//table的id
        // filename = filename;
        // debugger;
        if (getExplorer() == 'ie' || getExplorer() == undefined) {
            HtmlExportToExcelForIE(tableid, filename);
        }else {
            HtmlExportToExcelForEntire(tableid, filename)
        }
        layer.close(layer.index);
    }, function(){
        layer.close(layer.index);
    });


}
//IE浏览器导出Excel
function HtmlExportToExcelForIE(tableid, filename) {
    var curTbl = document.getElementById(tableid);
    curTbl.style.border="1px";
    curTbl.style.backgroundClip="padding-box";
    curTbl.style.position="relative";

    var oXL;
    try{
        oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
    }catch(e){
        alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel，"+"那么请调整IE的安全级别。\n\n具体操作：\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
        return false;
    }
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var Lenr = curTbl.rows.length;
    for (i = 0; i < Lenr; i++){
        var Lenc = curTbl.rows(i).cells.length;
        for (j = 0; j < Lenc; j++){
            oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;

        }

    }
    oXL.Visible = true;
}

//非IE浏览器导出Excel
var HtmlExportToExcelForEntire = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
        base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
        format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(table, name) {
        console.log(table.nodeType);
        if (!table.nodeType) {
            table = document.getElementById(table);
            // console.log(table);
        }
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = name + ".xls";
        document.getElementById("dlink").click();
    }
})()

function getExplorer() {
    var explorer = window.navigator.userAgent;
    //ie
    if (explorer.indexOf("MSIE") >= 0) {
        return 'ie';
    }
    //firefox
    else if (explorer.indexOf("Firefox") >= 0) {
        return 'Firefox';
    }
    //Chrome
    else if (explorer.indexOf("Chrome") >= 0) {
        return 'Chrome';
    }
    //Opera
    else if (explorer.indexOf("Opera") >= 0) {
        return 'Opera';
    }
    //Safari
    else if (explorer.indexOf("Safari") >= 0) {
        return 'Safari';
    }
}